package org.ellisande.grid.tags;

import java.util.EnumSet;

import javax.servlet.jsp.JspWriter;

import org.ellisande.grid.tags.support.TagBuilder;
import org.ellisande.tld.annotations.Attribute;
import org.ellisande.tld.annotations.Description;
import org.ellisande.tld.annotations.Info;

@Info("Defines a container to be rendered. Containers are styled divs.")
public class ContainerTag extends AbstractTag {

	private static final long serialVersionUID = -1227680077296226747L;
	
	private enum Type{
		DEFAULT(""),SIDEBAR("sidebar-nav");
		
		private String type;
		private Type(String type){
			this.type = type;
		}
		
		public String toString(){
			return type;
		}
		
		public String getStyle(){
			return type;
		}
		
		public boolean equals(Type type){
			return name().equals(type.name());
		}
	}

	@Attribute
	@Description("Defines the type of the container. Allowed values: default | sidebar. Default value: default.")
	private Type type = Type.DEFAULT;
	
	@Attribute
	@Description("Defines whether or not this container should be inset. If true, the \"well\" class is appened to the style. Allowed values: true | false. Default value: false.")
	private boolean inset = false;
	
	private static final String INSET_CLASS = "well";
	
	@Override
	public void startTag() throws Exception {
		TagBuilder div = createTagBuilder("div");
		div.addStyle(type.getStyle());
		if(inset){
			div.addStyle(INSET_CLASS);
		}
		if(Type.DEFAULT.equals(type)){
			div.addStyle("grid-well");
		}
		JspWriter out = pageContext.getOut();
		out.println(div.compileStartTag());

	}

	@Override
	public void endTag() throws Exception {
		TagBuilder div = createTagBuilder("div");
		JspWriter out = pageContext.getOut();
		out.println(div.compileEndTag()); 

	}
	
	public void setType(String containerType){
		for(Type type : EnumSet.allOf(Type.class)){
			if(type.name().equalsIgnoreCase(containerType)){
				this.type = type;
			}
		}
	}
	
	public void setInset(String containerInset){
		this.inset = Boolean.parseBoolean(containerInset);
	}

}
